#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'syxbyi'
__version__ = '1.0.0'

import vimrc.config
import templates.config
import argparse
import shutil
from os.path import dirname, splitext, join

def parse():
    parser = argparse.ArgumentParser(description = 'Create a file that contains your template in this directory, and tell me the name. I will copy this file into vim templates directory and update vimrc file.')
    parser.add_argument('template', help = 'template file')
    args = parser.parse_args()
    name = args.template
    return name

def main():
    current = dirname(__file__)
    name = parse()
    _, ext = splitext(name)
    vimrc_template_file = join(join(current, 'vimrc'), 'vimrcTemplates')
    with open(vimrc_template_file, 'r') as f:
        content = f.read()
    with open(vimrc_template_file, 'a') as f:
        string = 'au BufNewFile *%s 0r ~/.vim/templates/%s\n' % (ext, name)
        if content.find(string) == -1:
            f.write(string)
    vimrc.config.main()
    shutil.move(join(current, name), join(join(current, 'templates'), join('src', name)))
    templates.config.main()
    return

if __name__ == '__main__':
    main()

